<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/2/27
 * Time: 16:56
 */
class PurviewModel extends BaseModel{

    protected $tableName = "purview";//数据库表名

    protected $_validate = array(
        array('purview_name','require','权限名称不能为空！',1),
        array('purview_url','require','权限代码不能为空！',1),
        array('purview_url','','权限代码已存在！',0,'unique',3),
    );

    public function getPurviewList(){

        $return = array();
        $res = $this->where('del=1')->select();

        foreach($res as $k=>$val){
            $key = explode('/',$val['purview_url']);
            $key = $key[0];
            $return[$key][$k]['url']    = $val['purview_url'];
            $return[$key][$k]['name']   = $val['purview_name'];
            $return[$key][$k]['id']     = $val['purview_id'];
        }
        return $return;

    }
    /**
     * @param $post
     * @return array
     * 添加权限
     */
    public function addPurview($post){

        $return = array('status'=>'','info'=>'');
        $data  = $this->returnData($post);

        if(!$this->create($data)){
            $return['status'] = false;
            $return['info']   = $this->getError();
            return $return;
        }
        $res = $this->add($data);
        if(!$res){
            $return['status'] = false;
            $return['info']   = $this->getDbError();
        }else{
            $return['status'] = true;
            $return['info']   = "添加成功";
        }
        return $return;
    }
    /**
     * @param $post
     * @return array
     * 修改权限
     */
    public function editPurview($post){

        $return = array('status'=>'','info'=>'');
        $data  = $this->returnData($post);

        if(!$this->create()){
            $return['status'] = false;
            $return['info']   = $this->getError();
            return $return;
        }
        $res = $this->save($post);
        if(!$res){
            $return['status'] = false;
            $return['info']   = $this->getDbError();
        }else{
            $return['status'] = true;
            $return['info']   = "更新成功";
        }
        return $return;
    }
    /**
     * @param $post
     * @return array
     * 删除权限
     */
    public function deletePurview($id){

        $return = array('status'=>'','info'=>'');

        if(self::Teacher_id == $_SESSION['USER']['id']){
            $res = $this->where('purview_id='.$id)->delete();
            if($res){
                $return['status'] = true;
                $return['info']   = "删除成功";
            }else{
                $return['status'] = false;
                $return['info']   = "删除失败";
            }
        }else{
            $return['status'] = false;
            $return['info']   = "无权限";
        }
        return  $return;
    }


    /**
     * @param $post
     * @return array
     *返回数据
     */
    protected function returnData($post){

        $return['purview_id']   = !empty($post['purview_id'])  ? intval($post['purview_id']) : null;
        $return['purview_name'] = !empty($post['purview_name'])? addslashes($post['purview_name']) : '';
        $return['purview_url']  = !empty($post['purview_name'])? addslashes($post['purview_url']) : '';
        $return['purview_url']  = strtolower($return['purview_url']);
        $return['del']          = 1;

        return $return;
    }
}